package com.gr.staffpm.tasks.model;

import org.apache.wicket.model.LoadableDetachableModel;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.tasks.service.TaskService;


public class DetachableTaskModel extends LoadableDetachableModel<Task> {

    private final int id;
    private ProjectService projectService = null;
    private TaskService taskService = null;
    private final Task task;
    private static final long serialVersionUID = 1L;

    public DetachableTaskModel(ProjectService ps, Task t) {
        this.task = t;
        this.projectService = ps;
        this.id = task.getTaskId();
    }

    public DetachableTaskModel(TaskService taskService, Task task) {
        this.taskService = taskService;
        this.task = task;
        this.id = task.getTaskId();
    }

    @Override
    public int hashCode() {
        return Long.valueOf(id).hashCode();
    }

    /**
     * Used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     * 
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (obj instanceof DetachableTaskModel) {
            DetachableTaskModel other = (DetachableTaskModel) obj;
            return other.id == id;
        }
        return false;
    }


    @Override
    protected Task load() {
        if (taskService == null)
            return projectService.getTask(id);
        else
            return taskService.getTask(id);
    }

}
